//
//  CYRechargeMoneyBtn.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/6.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYSelectBtn.h"
@interface CYSelectBtn()<UITextFieldDelegate>{
    selectBlock _myblock;
}
@property (nonatomic, strong) UIColor *defaultColor;//未选中状态，边框的颜色
@property (nonatomic, strong) UIColor *selectColor;//选中状态，边框的颜色
@property (nonatomic, assign) CGFloat font_size;//字体大小
@property (nonatomic, copy) NSString *selectImage;//选中的image名字
@property (nonatomic, strong) CYImageView *selectIMG;//选中的image
@property (nonatomic, strong) UITextField *textfield;//输入框
@end
@implementation CYSelectBtn

- (instancetype)initWithTitle:(NSString *)title size:(CGFloat)size selectImage:(NSString *)imageName block:(selectBlock)block{
    self = [super init];
    if (self) {
        _defaultColor = ColorHex(0x888888);
        _selectColor = THEME_COLOR;
        _selectImage = imageName;
        _font_size = size;
        _money = title;
        _myblock = block;
        self.showTextfield = NO;
        
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:_defaultColor forState:UIControlStateNormal];
        [self setTitleColor:_selectColor forState:UIControlStateSelected];
        self.titleLabel.font = [UIFont systemFontOfSize:_font_size];
        self.layer.borderColor = _defaultColor.CGColor;
        self.layer.borderWidth = 0.5;
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = AL(5);
        
    }
    return self;
}
- (CYImageView *)selectIMG{
    if (!_selectIMG) {
        _selectIMG = [[CYImageView alloc]initWithImageName:_selectImage size:CGSizeMake(AL(PT(43)), AL(PT(36))) isRadius:NO];
        [self addSubview:_selectIMG];
        
        [_selectIMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).with.offset(0);
            make.bottom.equalTo(self.mas_bottom).with.offset(0);
            make.size.mas_equalTo(_selectIMG.bounds.size);
        }];
    }
    return _selectIMG;
}

- (UITextField *)textfield{
    if ((!_textfield)) {
        _textfield = [[UITextField alloc]init];
        [self setupTextfield];
        [self addSubview:_textfield];
        
        [_textfield mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0));
        }];
    }
    return _textfield;
}

- (void)setupTextfield{
    _textfield.textColor = _selectColor;
    _textfield.placeholder = @"自定义金额";
    [_textfield setValue:[UIFont systemFontOfSize:_font_size] forKeyPath:@"_placeholderLabel.font"];//设置placeholder的字体
    self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; // 设置上下位置
    _textfield.textAlignment = NSTextAlignmentCenter;
    _textfield.keyboardType = UIKeyboardTypeASCIICapable;
    _textfield.delegate = self;
}

- (void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    self.layer.borderColor = (selected)?_selectColor.CGColor:_defaultColor.CGColor;
    self.selectIMG.hidden = (selected)?NO:YES;
    if (self.showTextfield)
        self.textfield.text = (selected)?self.textfield.text:@"";//选中其他金额按钮的时候，让自定义金额text为空，显示placeholder
}

- (void)setShowTextfield:(BOOL)showTextfield{
    if (_showTextfield != showTextfield)
        _showTextfield = showTextfield;
    if (showTextfield)
        self.textfield.hidden = NO;
}


#pragma mark - uitextfield代理
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self endEditing:YES];
    _money = textField.text;  //判断文本框输入的文本后，赋值给控件的金额属性
    if (_myblock)
        _myblock(self);
    return YES;
}

//文本输入框编辑状态，控件的选中效果
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    self.layer.borderColor = _selectColor.CGColor;
    self.selectIMG.hidden = NO;
    _myblock(self);
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (![NSString judgeInputIsNumber:string])//不是数字，禁止输入
        return NO;
    else if (textField.text.length == 0 && [string isEqualToString:@"0"])//第一个数字不能为0
        return NO;
    else if (textField.text.length <= 8 && string.length > 0){//长度限制为8位数，即百万
        NSString *new_string = [textField.text stringByAppendingString:string];
        NSString *new_str = [new_string stringByReplacingOccurrencesOfString:@"," withString:@""];
        textField.text = [NSString commaBetweenThreeCharacters:new_str];
        _money = textField.text;
        _myblock(self);
        return NO;
    }
    else if (textField.text.length > 8 && string.length > 0)//超过8位，禁止输入
        return NO;
    else//可以删除
        return YES;
    
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
